<!--
@name:dialogScale
@author:Zhenglei
@date:15:50
@description:dialogScale
@update:2022/8/915:50
-->
<template>
  <el-dialog :title="title"
             :visible.sync="visible"
             :before-close="modalClose"
             :close-on-click-modal="false"
             :close-on-press-escape="false"
             width="50%">
    <el-form :model="subData"
             label-position="right"
             label-width="150px"
             ref="formModelRef"
             :rules='formModelRules'>
      <el-form-item label="量表：" prop="scaleId" :rules="isEdit?[{required:false}]:formModelRules.scaleId">
        <el-select v-model="subData.scaleId"
                   filterable
                   class="w-300"
                   value-key="id"
                   :disabled="isEdit"
                   @change="handleChangeScale"
                   :placeholder="placeholderScale">
          <el-option v-for="item in scaleDataSource"
                     :key="item.id"
                     :label="item.scaleName"
                     :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="结论：">
        <el-select v-model="subData.relationId"
                   filterable
                   :disabled="isEdit"
                   class="w-300"
                   multiple
                   :placeholder="placeholderEnding">
          <el-option v-for="item in endingDataSource"
                     :key="item.id"
                     :label="item.result"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>


      <el-form-item label="健康建议：">
        <vue-neditor-wrap :destroy="true"
                          v-model="subData.suggest"
                          :config="myConfig"></vue-neditor-wrap>
      </el-form-item>


    </el-form>
    <p align="center">
      <el-button @click="modalClose"
                 type="info">取 消
      </el-button>
      <el-button type="primary"
                 v-prevent-re-click
                 @click="handleSave()">保 存
      </el-button>
    </p>

  </el-dialog>

</template>

<script>
import VueNeditorWrap from "vue-neditor-wrap";

export default {
  name: "dialogScale",
  props: {
    dataObj: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      myConfig: {
        // 你的UEditor资源存放的路径,相对于打包后的index.html
        UEDITOR_HOME_URL: this.api.uEditorHomeUrl,
        // 编辑器不自动被内容撑高
        autoHeightEnabled: false,
        // 初始容器高度
        initialFrameHeight: '418',
        // 初始容器宽度
        initialFrameWidth: '100%',
        // 关闭自动保存
        enableAutoSave: false,
        scaleEnabled: true
      },
      title: '新 增',
      subData: {
        id: null,
        scaleId: '',
        relationId: [],
        suggest: '',
      },
      isEdit: false,
      placeholderScale: '请选择量表',
      placeholderEnding: '请选择结论',
      scaleDataSource: [],
      endingDataSource: [],
      judgeMode: '',//所选择的量表类型
      formModelRules: {
        scaleId: [{required: true, message: '请选择量表', trigger: 'blur'},],
      }
    }
  },
  created() {
    if (this.dataObj.id) {
      this.title = '编 辑'
      this.isEdit = true
      this.placeholderScale = this.dataObj.scaleName
      if (this.dataObj.scaleResult) {
        this.placeholderEnding = this.dataObj.scaleResult
      }
      if (this.dataObj.suggest) {
        this.subData.suggest = this.dataObj.suggest
      }
    } else {
      this.isEdit = false
      this.title = '新 增'
    }
    this.getScaleList()
  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false);
    },
    //选择量表
    handleChangeScale(val) {
      this.subData.relationId = []
      if (val.judgeMode === 1) {
        //一般量表
        this.judgeMode = 1
        this.endingDataSource = val.results
      } else if (val.judgeMode === 2) {
        //脑卒中量表
        this.judgeMode = 2
        this.endingDataSource = val.relationJudges
      }
    },
    //查询量表
    getScaleList() {
      this.post(this.api.healthMap.getScaleList).then((res) => {
        if (res.data.code === '200') {
          this.scaleDataSource = res.data.object
        }
      })
    },
    handleSave() {
      this.$refs.formModelRef.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            //编辑
            this.post(this.api.healthMap.updateSuggest, {
              id: this.dataObj.id,
              suggest: this.subData.suggest
            }).then((res) => {
              if (res.data.code === '200') {
                this.$message.success('更新成功!');
                this.$parent.getList();
                this.modalClose();
              }
            })
          } else {
            //新增
            let arr = []
            if (this.subData.relationId.length === 0) {
              //没有选择状态，把targetId作为relationId上传
              arr.push({
                type: 30,
                relationId: this.subData.scaleId.id,
                suggest: this.subData.suggest
              })
            } else {
              //选择了状态
              this.subData.relationId.forEach(x => {
                arr.push({
                  type: 31,
                  relationType:this.judgeMode,
                  relationId: x,
                  suggest: this.subData.suggest
                })
              })
            }
            this.post(this.api.healthMap.addSuggest, {
              jsonData: arr,
              searchType: 3
            }).then((res) => {
              if (res.data.code === '200') {
                this.$message.success('保存成功!');
                this.$parent.getList();
                this.modalClose();
              }
            })

          }
        }
      })
    }
  },
  components: {
    VueNeditorWrap
  }
}
</script>

<style scoped>

</style>
